#include <iostream>
#include <vector>
#include <string>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

const int number = 10;

void *threadstart(void *args)
{
    int cnt = 5;
    std::string name = static_cast<const char *>(args);
    while (cnt--)
    {
        std::cout << name << "run success" << std::endl;
        sleep(1);
    }

    return (void *)args;
}

int main()
{
    std::vector<pthread_t> tids;
    for (int i = 0; i < number; i++)
    {
        pthread_t tid;
        char *name = new char[128];
        sprintf(name, "thread-%d", i + 1);
        // std::cout << "-------------------------------" <<std::endl;
        // std::cout << name << std::endl;
        // std::cout << "-------------------------------" <<std::endl;
        pthread_create(&tid, nullptr, threadstart, (void *)name);
        tids.push_back(tid);
    }

    for (int i = 0; i < tids.size(); i++)
    {
        char *str = nullptr;
        pthread_join(tids[i], (void **)&str);

        std::cout << (const char *)str << "join success" << std::endl;
    }

    return 0;
}

// void *threadstart(void *args)
// {
//     while (true)
//     {
//         sleep(1);
//         std::cout << "new thread running, pid:" << getpid() << "--" << (char *)args << std::endl;
//     }
// }

// int main()
// {
//     pthread_t tid;
//     int a = 5;
//     int *b = &a;
//     char* name = "hello";
//     pthread_create(&tid, nullptr, threadstart, name);

//     while (true)
//     {
//         std::cout << "main thread running, pid:" << getpid() << std::endl;
//         sleep(1);
//     }

//     return 0;
// }